﻿using System.Windows.Forms;
using Fawcett.Plugins;
using System.Timers;
using System;
using System.IO.Ports;
using System.Collections.Generic;
using System.Configuration;
using CommandMessenger;

namespace Fawcett.Plugins.Arduino
{

    public class Arduino : CPlugin
    {
        private List<ArduinoBoard> Boards = new List<ArduinoBoard>();
        private RibbonButton btnList = new System.Windows.Forms.RibbonButton();
        private RibbonButton btnChannel = new System.Windows.Forms.RibbonButton();
        public Arduino()
        {
            LoadBoards();

            Boards.Add(new ArduinoBoard(1, "1.0.0.1")); /// FUDGE
            Boards.Add(new ArduinoBoard(2, "1.0.1.1")); //  Remove Later
            Boards.Add(new ArduinoBoard(3, "1.1.0.1")); ///
        }

        private void LoadBoards()
        {
            
            string[] ports = SerialPort.GetPortNames();

            foreach (string port in ports)
            {
                SerialPortManager _serialPortManager = new SerialPortManager()
                {
                    CurrentSerialSettings = { PortName = port, BaudRate = 115200 } // object initializer
                };

                if (_serialPortManager != null)
                {
                    ArduinoBoard board = new ArduinoBoard( new CmdMessenger(_serialPortManager) );

                    Boards.Add( board );
                }
            }


        }
        public override RibbonTab SetupMenu( RibbonTab HomeTab)
        {
            RibbonTab NewTab = new System.Windows.Forms.RibbonTab(Name);
            RibbonPanel AddonPanel = new RibbonPanel("Boards"); 

            btnList.Image = ListForm<List<ArduinoBoard>>.Image;
            btnList.Text = "List";
            btnList.Click += new EventHandler(this.List);

            btnChannel.Image = Resources.System.ToBitmap() ;
            btnChannel.Text = "Channels";
            btnChannel.Click += new EventHandler(this.Channel);

            AddonPanel.Items.Add(btnList);
            AddonPanel.Items.Add(btnChannel);

            AddonPanel.ButtonMoreEnabled = false;
            AddonPanel.ButtonMoreVisible = false;


            NewTab.Panels.Add(AddonPanel);


            return NewTab;
        }

        private void Channel(object sender, EventArgs e)
        {
            // TODO Replace the Configuration list with a list from the actual Channel Manager, so we can display the I/O Values

            ChannelCollection bar = (ChannelCollection)ConfigurationManager.GetSection("Channels");

            ListForm<ChannelCollection> nf = new ListForm<ChannelCollection>("List of Channels" , bar);

            if (nf.MdiParent == null) nf.MdiParent = this.parent;

            nf.Show();
        }

        private void List(object sender, EventArgs e)
        {
            ListForm<List<ArduinoBoard>> nf = new ListForm<List<ArduinoBoard>>("List of Connected Boards", Boards);

        }
    }
}
